﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class UnsettleWnd : Form
    {
        double fBuyTotal = 0;
        double fSellTotal = 0;
        bool bTableDrawn = false;

        string sCurrentAgentCode = "";
        string sCurrentBrokerCode = "";
        DateTime currentDate;

        public UnsettleWnd()
        {
            InitializeComponent();
        }

        private void UnsettleWnd_Load(object sender, EventArgs e)
        {
            try
            {
	            Config config = ConfigServices.GetConfigByName(Constants.CONFIG_NAME_DEPLOYMENT_SIDE);
	
	            if (config.Value == Constants.CONFIG_VALUE_DEPLOYMENT_SIDE_BROKER)
	            {
	                AgentCollection agentCollection = AgentServices.GetAgentCollection();
	
	                for (int iI = 0; iI < agentCollection.Count; iI++)
	                {
	                    comboBoxAgentCode.Items.Add(agentCollection[iI].Code);
	                }
	
	                config = ConfigServices.GetConfigByName(Constants.CONFIG_NAME_COMPANY_CODE);
	                comboBoxBrokerCode.Items.Add(config.Value);
	                comboBoxBrokerCode.SelectedIndex = 0;
	                comboBoxAgentCode.SelectedIndex = 0;
	
	                labelSellTotal.Text = "0";
	                labelBuyTotal.Text = "0";
	            }
	            else if (config.Value == Constants.CONFIG_VALUE_DEPLOYMENT_SIDE_AGENT)
	            {
	                BrokerCollection brokerCollection = BrokerServices.GetBrokerCollection();
	
	                for (int iI = 0; iI < brokerCollection.Count; iI++)
	                {
	                    comboBoxBrokerCode.Items.Add(brokerCollection[iI].Code);
	                }
	
	                config = ConfigServices.GetConfigByName(Constants.CONFIG_NAME_COMPANY_CODE);
	                comboBoxAgentCode.Items.Add(config.Value);
	                comboBoxAgentCode.SelectedIndex = 0;
	                comboBoxBrokerCode.SelectedIndex = 0;
	            }
            }
            catch (System.Exception ex)
            {
            	
            }

            //dataGridViewBuy.Width = (Size.Width - 60) / 2;
            //dataGridViewBuy.Height = Size.Height - 140;

            //dataGridViewSell.Left = (Width - 10) / 2;
            //dataGridViewSell.Width = (Size.Width - 60) / 2;
            //dataGridViewSell.Height = Size.Height - 140;

            WindowState = FormWindowState.Maximized;
        }

        private void UnsettleWnd_SizeChanged(object sender, EventArgs e)
        {
            dataGridViewBuy.Width = (Size.Width - 75) / 2;
            dataGridViewBuy.Height = Size.Height - 170;
            dataGridViewBuy.Left = 20;

            dataGridViewSell.Width = (Size.Width - 75) / 2;
            dataGridViewSell.Height = Size.Height - 170;
            dataGridViewSell.Left = dataGridViewSell.Width + 40;

            //MessageBox.Show(" " + dataGridViewBuy.Left + " " + dataGridViewBuy.Right + " " + dataGridViewSell.Left + " " + dataGridViewSell.Right + " " + Width);

            int iClearance = 80;
            labelSellTotal.Top = Height - iClearance;
            labelBuyTotal.Top = Height - iClearance;
            labelTotal1.Top = Height - iClearance;
            labelTotal2.Top = Height - iClearance;
            buttonSellSettle.Top = Height - iClearance - 2;
            buttonBuySettle.Top = Height - iClearance - 2;
            labelSellTotal.Left = dataGridViewSell.Left + 60;
            labelTotal2.Left = dataGridViewSell.Left;
            buttonSellSettle.Left = dataGridViewSell.Left + 160;
            labelBuyTotal.Left = dataGridViewBuy.Left + 60;
            labelTotal1.Left = dataGridViewBuy.Left;
            buttonBuySettle.Left = dataGridViewBuy.Left + 160;
            labelBuySide.Top = dataGridViewBuy.Top - 20;
            labelSellSide.Top = dataGridViewSell.Top - 20;
            labelBuySide.Left = dataGridViewBuy.Left;
            labelSellSide.Left = dataGridViewSell.Left;

            if (bTableDrawn)
            {
                dataGridViewSell.Columns[0].Width = 50;
                dataGridViewSell.Columns[1].Width = 150;

                dataGridViewSell.Columns[5].Width = dataGridViewSell.Width -
                    (200
                    + dataGridViewSell.Columns[2].Width
                    + dataGridViewSell.Columns[3].Width
                    + dataGridViewSell.Columns[4].Width
                    + 2);

                dataGridViewBuy.Columns[0].Width = 50;
                dataGridViewBuy.Columns[1].Width = 150;

                dataGridViewBuy.Columns[5].Width = dataGridViewBuy.Width -
                (200
                + dataGridViewBuy.Columns[2].Width
                + dataGridViewBuy.Columns[3].Width
                + dataGridViewBuy.Columns[4].Width
                + 2);
            }
        }

        private void buttonGO_Click(object sender, EventArgs e)
        {
            int iCount = dataGridViewSell.Columns.Count;
            for (int iI = 0; iI < iCount; iI++)
            {
                dataGridViewSell.Columns.RemoveAt(0);
            }

            iCount = dataGridViewBuy.Columns.Count;
            for (int iI = 0; iI < iCount; iI++)
            {
                dataGridViewBuy.Columns.RemoveAt(0);
            }

            DataGridViewCheckBoxColumn Chk1 = new DataGridViewCheckBoxColumn();
            Chk1.HeaderText = "Select";
            Chk1.Name = "Chk1";
            dataGridViewBuy.Columns.Add(Chk1);
            dataGridViewBuy.Columns["Chk1"].DisplayIndex = 0;

            DataGridViewCheckBoxColumn Chk2 = new DataGridViewCheckBoxColumn();
            Chk2.HeaderText = "Select";
            Chk2.Name = "Chk2";
            dataGridViewSell.Columns.Add(Chk2);
            dataGridViewSell.Columns["Chk2"].DisplayIndex = 0;

            sCurrentAgentCode = comboBoxAgentCode.Text;
            sCurrentBrokerCode = comboBoxBrokerCode.Text;
            currentDate = dateTimePicker.Value;

            try
            {
	            dataGridViewBuy.DataSource = DailyTransactionsServices.GetUnSettleDataTable(dateTimePicker.Value,
	                comboBoxBrokerCode.Text, comboBoxAgentCode.Text, "B");
	
	            dataGridViewSell.DataSource = DailyTransactionsServices.GetUnSettleDataTable(dateTimePicker.Value,
	                comboBoxBrokerCode.Text, comboBoxAgentCode.Text, "S");
            }
            catch (System.Exception ex)
            {
            	
            }

            for (int iI = 0; iI < dataGridViewBuy.Columns.Count; iI++)
            {
                dataGridViewBuy.Columns[iI].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            for (int iI = 0; iI < dataGridViewSell.Columns.Count; iI++)
            {
                dataGridViewSell.Columns[iI].SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            bTableDrawn = true;

            fBuyTotal = 0;
            fSellTotal = 0;
            labelSellTotal.Text = "0.00";
            labelBuyTotal.Text = "0.00";

            UnsettleWnd_SizeChanged(null, null);
        }

        private void dataGridViewBuy_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridViewBuy.CurrentCell.ColumnIndex != 0)
            {
                return;
            }

            if (dataGridViewBuy.CurrentCell.Value == null
                || (bool)dataGridViewBuy.CurrentCell.Value == false)
            {
                dataGridViewBuy.CurrentCell.Value = true;
                fBuyTotal += double.Parse(dataGridViewBuy.CurrentRow.Cells[4].Value.ToString());
                labelBuyTotal.Text = fBuyTotal.ToString();
            }
            else
            {
                dataGridViewBuy.CurrentCell.Value = false;
                fBuyTotal -= double.Parse(dataGridViewBuy.CurrentRow.Cells[4].Value.ToString());
                labelBuyTotal.Text = fBuyTotal.ToString();
            }
        }

        private void dataGridViewSell_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridViewSell.CurrentCell.ColumnIndex != 0)
            {
                return;
            }

            if (dataGridViewSell.CurrentCell.Value == null
                || (bool)dataGridViewSell.CurrentCell.Value == false)
            {
                dataGridViewSell.CurrentCell.Value = true;
                double value = double.Parse(dataGridViewSell.CurrentRow.Cells[4].Value.ToString());
                fSellTotal = value + fSellTotal;
                labelSellTotal.Text = String.Format("{0:0.00}", fSellTotal);
            }
            else
            {
                dataGridViewSell.CurrentCell.Value = false;
                fSellTotal -= double.Parse(dataGridViewSell.CurrentRow.Cells[4].Value.ToString());
                labelSellTotal.Text = String.Format("{0:0.00}", fSellTotal);
            }
        }

        private void buttonBuySettle_Click(object sender, EventArgs e)
        {
            bool bFailed = false;
            bool bUnSettled = false;
            int iCount = dataGridViewBuy.Rows.Count;
            for (int iI = 0; iI < iCount; iI++)
            {
                if (dataGridViewBuy.Rows[iI].Cells[0].Value != null &&
                    (bool)dataGridViewBuy.Rows[iI].Cells[0].Value == true)
                {
                    string clientCode = dataGridViewBuy.Rows[iI].Cells[1].Value.ToString();
                    try
                    {
                        string sReferenceNO = dataGridViewBuy.Rows[iI].Cells[5].Value.ToString();
                        DailyTransactionsServices.UnSettle(clientCode, currentDate, sCurrentBrokerCode, "B", sReferenceNO);

                        LoanAccountServices.Delete(sReferenceNO);

                        bUnSettled = true;
                    }
                    catch (System.Exception ex)
                    {
                        bFailed = true;
                    }
                    finally
                    {
                        dataGridViewBuy.Rows[iI].Cells[0].Value = false;
                        fBuyTotal -= double.Parse(dataGridViewBuy.Rows[iI].Cells[4].Value.ToString());
                        labelBuyTotal.Text = String.Format("{0:0.00}", fBuyTotal);
                    }
                }
            }
            if (bFailed)
            {
                MessageBox.Show("Updating database failed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (bUnSettled)
            {
                MessageBox.Show("Un-Settling successful");
            }
            else
            {
                MessageBox.Show("No entries selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            ResetTables();
        }

        private void buttonSellSettle_Click(object sender, EventArgs e)
        {
            int iCount = dataGridViewSell.Rows.Count;
            bool bFailed = false;
            bool bUnSettled = false;
            for (int iI = 0; iI < iCount; iI++)
            {
                if (dataGridViewSell.Rows[iI].Cells[0].Value != null &&
                    (bool)dataGridViewSell.Rows[iI].Cells[0].Value == true)
                {
                    string clientCode = dataGridViewSell.Rows[iI].Cells[1].Value.ToString();
                    try
                    {
                        string sReferenceNO = dataGridViewSell.Rows[iI].Cells[5].Value.ToString();
                        DailyTransactionsServices.UnSettle(clientCode, currentDate, sCurrentBrokerCode, "S", sReferenceNO);

                        LoanAccountServices.Delete(sReferenceNO);
                        bUnSettled = true;
                    }
                    catch (System.Exception ex)
                    {
                        bFailed = true;
                    }
                    finally
                    {
                        dataGridViewSell.Rows[iI].Cells[0].Value = false;
                        fSellTotal -= double.Parse(dataGridViewSell.Rows[iI].Cells[4].Value.ToString());
                        labelSellTotal.Text = String.Format("{0:0.00}", fSellTotal);
                    }
                }
            }
            if (bFailed)
            {
                MessageBox.Show("Updating database failed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (bUnSettled)
            {
                MessageBox.Show("Un-Settling successful");
            }
            else
            {
                MessageBox.Show("No entries selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            ResetTables();
        }

        private void ResetTables()
        {
            comboBoxBrokerCode.Text = sCurrentBrokerCode;
            comboBoxAgentCode.Text = sCurrentAgentCode;
            dateTimePicker.Value = currentDate;
            buttonGO_Click(null, null);
        }
    }
}
